﻿var mpost = {
    //表格初始化
    gridInit: function () {
        $("#mpost-grid").datagrid({
            url: "/manage/post/grid",
            nowrap: false,
            rownumbers: true,
            border: false,
            fit: true,
            singleSelect: true,
            pageSize: 20,
            pagination: true,
            idField: "ID",
            toolbar: "#mpost-tools",
            columns:
            [[
                { field: "Name", title: "名称", width: 120, align: "center" },
                { field: "Remark", title: "描述", width: 280, align: "center" },
                { field: "Sequence", title: "排序", width: 50, align: "center" },
                { field: "InsertTime", title: "添加时间", width: 140, align: "center" }
            ]]
        });
    },

    //添加
    insert: function () {
        mpost.mutual("添加", null);
    },
    //修改
    update: function () {
        var row = $("#mpost-grid").datagrid("getSelected");
        if (row) {
            mpost.mutual("修改", row.ID);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#mpost-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/post/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { mpost.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id) {
        var href = "/manage/post/page?" + $.param({ id: id });
        var d = manage.dialog({
            title: title, href: href, width: 500,
            handler: function () {
                manage.form({
                    dialog: d, formid: "#mpost-form",
                    url: "/manage/post/save",
                    success: function (result) { mpost.reload(); }
                });
            },
            onLoad: function () { mpost.formInit(); }
        });
    },
    //弹窗初始化
    formInit: function () {

    },

    //排序上移
    desc: function () {
        mpost.order("desc");
    },
    //排序下移
    asc: function () {
        mpost.order("asc");
    },
    //排序操作
    order: function (order) {
        var row = $("#mpost-grid").datagrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/post/setorder",
                type: "post", data: { id: row.ID, order: order },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mpost-grid").datagrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //刷新列表
    reload: function () {
        $("#mpost-grid").datagrid("unselectAll");
        $("#mpost-grid").datagrid("reload");
    }
};

$(function () { mpost.gridInit(); });